package niconama04.client;

import java.util.ArrayList;
import java.util.List;

import niconama04.shared.data.Category;
import niconama04.shared.data.CategoryOriginal;
import niconama04.shared.data.Original;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;


public class RegistOriginal implements EntryPoint {

	/** Create a remote service proxy to talk to the server-side service. */
	final RegistOriginalServiceAsync service = GWT.create(RegistOriginalService.class);

	Button btnO;

	Button btnC;

	Label label;

	FlexTable table;

	FlexTable mainTable;

	Label subLabel;

	FlexTable subTable;

	List<Original> originals;

	List<Category> categorys;

	/**
	 * This is the entry point method.
	 */
	@Override
	public void onModuleLoad() {

		ViewBuilder.build();

		{
			categorys = new ArrayList<Category>();
			service.getCategorys(new AsyncCallback<List<Category>>() {

				@Override
				public void onFailure(Throwable x_caught) {
				}

				@Override
				public void onSuccess(List<Category> x_result) {
					for (Category c : x_result) {
						categorys.add(c);
					}
				}
			});
		}
		{
			originals = new ArrayList<Original>();
			service.getOriginals(new AsyncCallback<List<Original>>() {

				@Override
				public void onFailure(Throwable x_caught) {
				}

				@Override
				public void onSuccess(List<Original> x_result) {
					for (Original o : x_result) {
						originals.add(o);
					}
				}
			});
		}

		{
			RootPanel rp = RootPanel.get("gwt_original");
			if (rp == null) {
				return;
			}

			label = new Label();
			table = new FlexTable();
			table.setBorderWidth(3);
			table.setCellPadding(5);

			VerticalPanel vp = new VerticalPanel();
			{
				rp.add(vp);
			}
			{
				HorizontalPanel buttons = new HorizontalPanel();
				btnO = new Button("Original");
				btnC = new Button("Category");
				buttons.add(btnO);
				buttons.add(btnC);
				vp.add(buttons);
			}
			{
				Label sep = new Label("");
				sep.setHeight("10px");
				vp.add(sep);
				vp.add(label);
				vp.add(table);
			}
		}
		{
			btnO.addClickHandler(new ClickHandler() {
				
				@Override
				public void onClick(ClickEvent x_event) {
					clearSub();
					loadOriginal();
				}
			});
			btnC.addClickHandler(new ClickHandler() {
				
				@Override
				public void onClick(ClickEvent x_event) {
					clearSub();
					loadCategory();
				}
			});
		}

		{
			RootPanel rp = RootPanel.get("gwt_original_category");
			if (rp == null) {
				return;
			}

			mainTable = new FlexTable();
			mainTable.setBorderWidth(3);
			mainTable.setCellPadding(5);
			subLabel = new Label();
			subTable = new FlexTable();
			subTable.setBorderWidth(3);
			subTable.setCellPadding(5);

			VerticalPanel vp = new VerticalPanel();
			{
				rp.add(vp);
			}
			{
				vp.add(mainTable);
				Label sep = new Label("");
				sep.setHeight("10px");
				vp.add(sep);
				vp.add(subLabel);
				vp.add(subTable);
			}
		}

		{
			loadOriginal();
		}
	}

	void loadOriginal() {
		service.getOriginals(new AsyncCallback<List<Original>>(){

			@Override
			public void onFailure(Throwable x_caught) {
				label.setText("Original");
				table.removeAllRows();
				table.setWidget(0, 0, new Label(x_caught.getMessage()));
			}

			@Override
			public void onSuccess(List<Original> x_result) {
				label.setText("Original");
				table.removeAllRows();
				int row = table.getRowCount();
				table.setWidget(row, 0, new Label("key"));
				table.setWidget(row, 1, new Label("pos"));
				table.setWidget(row, 2, new Label("baseform"));
				table.setWidget(row, 3, new Label(""));
				row++;
				for (final Original original : x_result) {
					final Long id = original.getKey();
					table.setWidget(row, 0, new Label(String.valueOf(original.getKey())));
					table.setWidget(row, 1, new Label(original.getPos().getName()+"("+original.getPos().getKey()+")"));
					table.setWidget(row, 2, new Label(original.getBaseform()));
					Button button = new Button("edit");
					button.addClickHandler(new ClickHandler() {

						@Override
						public void onClick(ClickEvent x_event) {
							{
								clearSub();
								mainTable.setWidget(0, 0, new Label(String.valueOf(original.getKey())));
								mainTable.setWidget(0, 1, new Label(original.getPos().getName()+"("+original.getPos().getKey()+")"));
								mainTable.setWidget(0, 2, new Label(original.getBaseform()));
							}
							loadSubCategory(id);
						}
					});
					table.setWidget(row, 3, button);
					row++;
				}
			}
		});
	}

	void loadCategory() {
		service.getCategorys(new AsyncCallback<List<Category>>(){

			@Override
			public void onFailure(Throwable x_caught) {
				label.setText("Category");
				table.removeAllRows();
				table.setWidget(0, 0, new Label(x_caught.getMessage()));
			}

			@Override
			public void onSuccess(List<Category> x_result) {
				label.setText("Category");
				table.removeAllRows();
				int row = table.getRowCount();
				table.setWidget(row, 0, new Label("key"));
				table.setWidget(row, 1, new Label("name"));
				table.setWidget(row, 2, new Label(""));
				row++;
				for (final Category category : x_result) {
					final Long categoryKey = category.getKey();
					table.setWidget(row, 0, new Label(String.valueOf(category.getKey())));
					table.setWidget(row, 1, new Label(category.getName()));
					Button button = new Button("edit");
					button.addClickHandler(new ClickHandler() {

						@Override
						public void onClick(ClickEvent x_event) {
							{
								clearSub();
								{
									mainTable.setWidget(0, 0, new Label(String.valueOf(category.getKey())));
									mainTable.setWidget(0, 1, new Label(category.getName()));
								}
							}
							loadSubOriginal(categoryKey);
						}
					});
					table.setWidget(row, 2, button);
					row++;
				}
			}
		});
	}

	void loadSubCategory(final Long x_originalKey) {
		service.getSubCategorys(x_originalKey, new AsyncCallback<List<Category>>(){

			@Override
			public void onFailure(Throwable x_caught) {
				subLabel.setText("Category");
				subTable.removeAllRows();
				subTable.setWidget(0, 0, new Label(x_caught.getMessage()));
			}

			@Override
			public void onSuccess(List<Category> x_result) {
				subLabel.setText("Category");
				subTable.removeAllRows();
				int row = subTable.getRowCount();
				subTable.setWidget(row, 0, new Label("key"));
				subTable.setWidget(row, 1, new Label("name"));
				subTable.setWidget(row, 2, new Label(""));
				row++;
				for (Category category : x_result) {
					final Long categoryKey = category.getKey();
					subTable.setWidget(row, 0, new Label(String.valueOf(category.getKey())));
					subTable.setWidget(row, 1, new Label(category.getName()));
					Button button = new Button("delete");
					button.addClickHandler(new ClickHandler() {

						@Override
						public void onClick(ClickEvent x_event) {
							CategoryOriginal co = new CategoryOriginal();
							co.setCategoryKey(categoryKey);
							co.setOriginalKey(x_originalKey);
							service.deleteCategoryOriginal(co, new AsyncCallback<Void>() {

								@Override
								public void onFailure(Throwable x_caught) {
									loadSubCategory(x_originalKey);
								}

								@Override
								public void onSuccess(@SuppressWarnings("hiding") Void x_result) {
									loadSubCategory(x_originalKey);
								}
							});
						}
					});
					subTable.setWidget(row, 2, button);
					row++;
				}
				{
					subTable.setWidget(row, 0, new Label(""));
					final ListBox listBox = new ListBox();
					listBox.addItem("", "");
					for (Category category : categorys) {
						listBox.addItem(category.getName(), String.valueOf(category.getKey()));
					}
					subTable.setWidget(row, 1, listBox);
					Button button = new Button("regist");
					button.addClickHandler(new ClickHandler() {

						@Override
						public void onClick(ClickEvent x_event) {
							int index = listBox.getSelectedIndex();
							String value = listBox.getValue(index);
							if (value == null || value.equals("")) {
								return;
							}
							Long categoryKey = Long.valueOf(value);
							CategoryOriginal co = new CategoryOriginal();
							co.setCategoryKey(categoryKey);
							co.setOriginalKey(x_originalKey);
							service.registCategoryOriginal(co, new AsyncCallback<CategoryOriginal>() {

								@Override
								public void onFailure(Throwable x_caught) {
									loadSubCategory(x_originalKey);
								}

								@Override
								public void onSuccess(@SuppressWarnings("hiding") CategoryOriginal x_result) {
									loadSubCategory(x_originalKey);
								}
							});
						}
					});
					subTable.setWidget(row, 2, button);
				}
			}
		});
	}

	void loadSubOriginal(final Long x_categoryKey) {
		service.getSubOriginals(x_categoryKey, new AsyncCallback<List<Original>>(){

			@Override
			public void onFailure(Throwable x_caught) {
				subLabel.setText("Original");
				subTable.removeAllRows();
				subTable.setWidget(0, 0, new Label(x_caught.getMessage()));
			}

			@Override
			public void onSuccess(List<Original> x_result) {
				subLabel.setText("Original");
				subTable.removeAllRows();
				int row = subTable.getRowCount();
				subTable.setWidget(row, 0, new Label("key"));
				subTable.setWidget(row, 1, new Label("pos"));
				subTable.setWidget(row, 2, new Label("baseform"));
				subTable.setWidget(row, 3, new Label(""));
				row++;
				for (Original original : x_result) {
					final Long originalKey = original.getKey();
					subTable.setWidget(row, 0, new Label(String.valueOf(original.getKey())));
					subTable.setWidget(row, 1, new Label(original.getPos().getName()+"("+original.getPos().getKey()+")"));
					subTable.setWidget(row, 2, new Label(original.getBaseform()));
					Button button = new Button("delete");
					button.addClickHandler(new ClickHandler() {

						@Override
						public void onClick(ClickEvent x_event) {
							CategoryOriginal co = new CategoryOriginal();
							co.setCategoryKey(x_categoryKey);
							co.setOriginalKey(originalKey);
							service.deleteCategoryOriginal(co, new AsyncCallback<Void>() {

								@Override
								public void onFailure(Throwable x_caught) {
									loadSubOriginal(x_categoryKey);
								}

								@Override
								public void onSuccess(@SuppressWarnings("hiding") Void x_result) {
									loadSubOriginal(x_categoryKey);
								}
							});
						}
					});
					subTable.setWidget(row, 3, button);
					row++;
				}
				{
					subTable.setWidget(row, 0, new Label(""));
					subTable.setWidget(row, 1, new Label(""));
					final ListBox listBox = new ListBox();
					listBox.addItem("", "");
					for (Original original : originals) {
						listBox.addItem(original.getBaseform(), String.valueOf(original.getKey()));
					}
					subTable.setWidget(row, 2, listBox);
					Button button = new Button("regist");
					button.addClickHandler(new ClickHandler() {

						@Override
						public void onClick(ClickEvent x_event) {
							int index = listBox.getSelectedIndex();
							String value = listBox.getValue(index);
							if (value == null || value.equals("")) {
								return;
							}
							Long originalKey = Long.valueOf(value);
							CategoryOriginal co = new CategoryOriginal();
							co.setCategoryKey(x_categoryKey);
							co.setOriginalKey(originalKey);
							service.registCategoryOriginal(co, new AsyncCallback<CategoryOriginal>() {

								@Override
								public void onFailure(Throwable x_caught) {
									loadSubOriginal(x_categoryKey);
								}

								@Override
								public void onSuccess(@SuppressWarnings("hiding") CategoryOriginal x_result) {
									loadSubOriginal(x_categoryKey);
								}
							});
						}
					});
					subTable.setWidget(row, 3, button);
				}
			}
		});
	}

	void clearSub() {
		mainTable.removeAllRows();
		subLabel.setText("");
		subTable.removeAllRows();
	}
}
